package com.cameron.ucenter.controller;

import com.cameron.entity.*;
import com.cameron.servicebase.config.exception.BusinessException;
import com.cameron.ucenter.entity.vo.AboutMeVo;
import com.cameron.ucenter.entity.vo.RegisterVo;
import com.cameron.ucenter.service.UcenterMemberService;
import com.cameron.utils.JwtUtils;
import com.cameron.utils.Result;
import com.hooper.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.cameron.constant.RoleConstant.*;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author cameron
 * @since 2022-06-30
 */
@RestController
@RequestMapping("/service-user/member")
@CrossOrigin
@Slf4j
public class UcenterMemberController {

    @Resource
    private UcenterMemberService memberService;


    @PostMapping("login")
    public Result login(@RequestBody UcenterMember member) {
        return memberService.login(member);
    }

    @PostMapping("register")
    public Result register(@RequestBody RegisterVo registerVo) {
        return memberService.register(registerVo);
    }

    @GetMapping("getMemberInfo")
    public Result getInfo(HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(memberId);
        return Result.ok().data("userInfo", member);
    }


    @GetMapping("count/register/{date}")
    public Integer countRegisterByDate(@PathVariable String date) {
        return memberService.countRegister(date);
    }


    @GetMapping("/aboutMe/{memberId}")
    public Result getAboutMe(@PathVariable String memberId) {
        AboutMeVo aboutMe = memberService.getAboutMe(memberId);
        return Result.ok().data("me", aboutMe);
    }

    @PostMapping("/update")
    public Result updateMember(@RequestBody MemberVo memberVo) {
        UcenterMember member = new UcenterMember();
        member.setId(memberVo.getId());
        member.setNickname(memberVo.getNickname());
        member.setSex(memberVo.getSex());
        member.setAge(memberVo.getAge());
        member.setAvatar(memberVo.getAvatar());
        String newToken = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return memberService.updateById(member) ? Result.ok().message("修改成功").data("token", newToken) : Result.error().message("修改失败");
    }

    @GetMapping("{id}")
    public Result getMemberById(@PathVariable String id) {
        UcenterMember member = memberService.getById(id);
        if (member == null) throw new BusinessException(40002, "该用户不存在！");
        MemberVo memberVo = new MemberVo();
        memberVo.setNickname(member.getNickname());
        memberVo.setId(id);
        memberVo.setAvatar(member.getAvatar());
        memberVo.setAge(member.getAge());
        memberVo.setSex(member.getSex());
        return Result.ok().data("member", memberVo);
    }

    @PostMapping("saveTeacherAsMember")
    public void saveTeacherAsMember(@RequestBody EduTeacher teacher) {
        String id = teacher.getId();
        String name = teacher.getName();
        String avatar = teacher.getAvatar();
        UcenterMember member = new UcenterMember();
        member.setNickname(name);
        member.setPassword("123456");
        member.setMobile(RandomUtil.randomPhoneNum());
        member.setAvatar(avatar);
        member.setAge(RandomUtil.randomInt(30, 60));
        member.setId(id);
        member.setMemberRoles(MEMBER_TEACHER);
        member.setSex(RandomUtil.randomInt(2));
        memberService.save(member);
    }

    @PostMapping("/changeRole/{memberId}/{role}")
    public boolean changeUserRole(@PathVariable("memberId") String memberId, @PathVariable("role") Integer role) {
        UcenterMember member = new UcenterMember();
        if (StringUtils.isEmpty(memberId)) {
            throw new BusinessException(40001, "参数为空！");
        }
        member.setId(memberId);
        if (role != MEMBER_TEACHER && role != NORMAL_MEMBER) {
            log.warn("{}-->this motherfucker tryna change to bad role", memberId);
            throw new BusinessException(40003, "错误的角色id");
        }
        member.setMemberRoles(role);
        return memberService.updateById(member);
    }

    @PostMapping("backend/login")
    public BackendMemberDto backendLogin(@RequestBody BackendLoginVo loginVo) {
        return memberService.backendLogin(loginVo.getUsername(), loginVo.getPassword());
    }

}


